<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * Smarty {block_new} function plugin
 * @param array|null $params
 * @param Smarty $smarty
 */
function smarty_function_block_news($params,$smarty){
	if (!isset($params['category_id'])){
		return ;
	}

	$smarty->CI->load->model('paper/paper_model');
	$categories = array();
	$categories = $smarty->CI->paper_model->get_categories(array('category_id'=>$params['category_id']),false,false);

	if (isset($params['limit'])){
		$limit = $params['limit'];
	}else{
		$limit = 3;
	}
	$html = "";

	foreach ($categories as $category){

		$html .="<div class=\"block-new\">"."\n";

		$html .="<div class=\"block-new-title\"><h3><span>{$category['name']}</span>"."\n";
			if (isset($params['show_readmore']) && $params['show_readmore'] == true){
				$html .="<a href=\"{$category[link]}\" title=\"Xem thêm\">Xem thêm</a>";
			}
		$html .= "</h3></div>"."\n";

		$news = array();
		$news = $smarty->CI->paper_model->get_papers(array('category_id'=>$category['category_id'],'limit'=>$limit));
		//var_dump($category['category_id']);
		$html .= "<div class=\"block-new-content\">"."\n"
			  ."<ul class=\"nlist\">"."\n";
		if (is_array($news) && count($news)>0){
			$i = 0;
			foreach ($news as $new){
				$html .= "<li class=\"nitem".((($i%2)==0)?' even':' odd').(($i==0)?' frist':(($i+1 == count($news))?' last':''))."\">";
				if (isset($params['show_image']) && $params['show_image'] === true){
					$html .="<div class=\"nimage\"><a href=\"$new[link]\" title=\"new\"><img src=\"$new[image]\" alt=\"$new[name]\" /></a></div>"."\n";
				}
				$html .="<div class=\"ninfor\">"."\n";
				$html .="<h2>";
				$html .= "<a href=\"{$new[link]}\" title=\"{$new[name]}\">{$new[name]}</a>";
				$html .="</h2>";
				if (isset($params['show_des']) && $params['show_des'] === true){
					$html .="<p>".substr_utf8($new[introtext],0,50)."</p>";
				}
				$html .="</div>"."\n";

				$html .= "</li>"."\n";
				$i++;
			}
		}
		$html .="</ul>"."\n";
		$html .="</div>"."\n";
		$html .="</div>"."\n";
	}
	return $html;
}
